package com.xiaodou.mjf.application.fee.service.impl;

import com.xiaodou.mjf.application.fee.repository.RepaymentPlanDetailRepository;
import com.xiaodou.mjf.application.fee.service.RepaymentPlanDetailService;
import com.xiaodou.mjf.entity.fee.RepaymentPlan;
import com.xiaodou.mjf.entity.fee.RepaymentPlanDetail;
import com.xiaodou.mjf.infrastructure.common.app.JpaSimpleService;

import org.joda.time.DateTime;
import org.joda.time.DateTimeFieldType;
import org.joda.time.format.DateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import java.util.List;


/**
 * RepaymentPlanDetail业务访问实现
 *
 * @author XiaoDou.com Code Generator
 * @version 1.0
 *          2014-10-29 17:37:52
 */
@Service
@Transactional
public class RepaymentPlanDetailServiceImpl extends JpaSimpleService<RepaymentPlanDetail, String> implements RepaymentPlanDetailService {

    Logger logger = LoggerFactory.getLogger(RepaymentPlanDetailServiceImpl.class);

    @Resource
    RepaymentPlanDetailRepository repaymentPlanDetailRepository;

    @Override
    protected JpaRepository<RepaymentPlanDetail, String> getRepository() {
        return repaymentPlanDetailRepository;
    }


    @Override
    public RepaymentPlanDetail findByAccountNumberAndStageEndTime(String accountNumber) {
        DateTime now = new DateTime();
        int year = now.get(DateTimeFieldType.year());
        int month = now.get(DateTimeFieldType.monthOfYear());
        DateTime dt1 = DateTimeFormat.forPattern("yyyy-M-d HH:mm:ss").parseDateTime(year + "-" + month + "-1 00:00:00");
        DateTime dt2 = dt1.plusMonths(1).minusMillis(1);
        //字段：stage_end_time between 本月第一天0点 and 本月最后一天24点
        return repaymentPlanDetailRepository.findByAccountNumberAndStageEndTime(accountNumber, dt1.toDate(), dt2.toDate());
    }

    @Override
    public List<RepaymentPlanDetail> findByAccountNumberAndInstallmentNo(String number, Integer installmentNo) {
        return repaymentPlanDetailRepository.findByAccountNumberAndInstallmentNo(number, installmentNo);
    }

	@Override
	public Double totalRepaymentAmount(String accountNumber,Integer installmentNo) {
		return repaymentPlanDetailRepository.totalRepaymentAmount(accountNumber,installmentNo);
	}
}

